﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SistemaAdministracion.Models.PatternBase
{
    class DbContextAdapter : IDbContext
    {
        private readonly DbContext _context;

        public DbContextAdapter(DbContext context)
        {
            this._context = context;
        }

        protected ObjectContext objectContext
        {
            get { return ((IObjectContextAdapter)_context).ObjectContext; }
        }

        public void Dispose()
        {
            _context.Dispose();
        }

        public System.Data.Entity.DbSet<T> CreateDbSet<T>() where T : class
        {
            return _context.Set<T>();
        }

        public void SaveChanges()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException e) { Console.WriteLine(e.Message); }
            
        }

        public void ChangeObjectState<T>(T Entity, System.Data.Entity.EntityState entityState) where T : class
        {
            objectContext.ObjectStateManager.ChangeObjectState(Entity, entityState);
        }
    }
}
